package com.example.grape_effect.effet.effect

import com.example.grape_effect.gl.common.ShaderParameter
import kotlin.math.sin

class HeartBeatEffect: LYEffect() {

    init {
        name = "hartBeatEffect"
    }

    override var fragmentShader = """
        precision highp float;
        varying vec2 textureCoordinate;
        uniform sampler2D inputTexture;
        uniform float intensity;

        void main()
         {
            vec2 dir = textureCoordinate - vec2(0.6,0.6);// 0.6 0.6 为心跳位置
        // 最终偏移的值：方向 * （1-长度），越靠外偏移越小
            vec2 offset = intensity * normalize(dir)* (1.0- length(dir) );
            vec2 uv =  textureCoordinate + offset;
            gl_FragColor = texture2D( inputTexture,uv);
         }

        ////计算偏移的方向
        //float2 dir = i.uv - _DistortCenter.xy;
        ////最终偏移的值：方向 * （1-长度），越靠外偏移越小
        //float2 offset = _DistortFactor * normalize(dir) * (1 - length(dir));
        ////计算采样uv值：正常uv值+从中间向边缘逐渐增加的采样距离
        //float2 uv = i.uv + offset;
        //return tex2D(_MainTex, uv);
    """.trimIndent()

    override fun addParameters() {
        super.addParameters()
        parameters["intensity"] = ShaderParameter(
            "intensity",
            ShaderParameter.TYPE_UNIFORM,
            ShaderParameter.VALUE_TYPE_FLOAT
        )
    }

    override fun setDefaultParameterValues() {
        super.setDefaultParameterValues()
        parameters["intensity"]?.value = 0.1f

    }

    fun setIntensity(intensity: Float) {
        parameters["intensity"]?.value = intensity
    }

    var amplitude = 0.005f //振幅
    fun updateIntensity(executedTime: Long) {
        val intensity = amplitude * sin((executedTime / 200f.toDouble())).toFloat()
        setIntensity(intensity)
    }

    override fun draw() {
        updateIntensity(System.currentTimeMillis() - startTime)
        super.draw()
    }


}